package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import Backend.AlgorithmManager;
import Backend.ImageOutPut;
import Backend.Algorithmen.SuperAlgo;

public class MazeListener implements ActionListener{

	
	private ControlPanel cpanel;
	private StatusBar sbar;
	private MazeArea marea;
	private AlgorithmManager amanger = new AlgorithmManager();
	
	public void setData(ControlPanel c, StatusBar s, MazeArea m){
		cpanel = c;
		sbar = s;
		marea = m;
	}
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Worker tmp = new Worker();
		tmp.start();
	}
	

	
	private class Worker extends Thread{
		
		private SuperAlgo algo = null;
		
		@Override
		public void run() {
			cpanel.enableStartButton(false);
			sbar.setOnlyStatus("Rendering ...");
			int x = cpanel.getXCount();
			int y = cpanel.getYCount();
			marea.setWH(x, y);
						
			algo = cpanel.getAlgorithm();
			algo.setStatus(0);
			algo.setGrid(marea.getGrid());
			algo.setXYCount(x, y);
			algo.setDelay(cpanel.getDelay());
			
			algo.setRunning(true);

			Thread t = new Thread(r);
			t.start();
			algo.start(cpanel.getSpeed());
			
		
			
			sbar.clear();
			cpanel.enableStartButton(true);
			
			sbar.setOnlyStatus("Needed Time: " + algo.getTime() + " ms");
			
		//	ImageOutPut.generatePicture(new File("/home/basti/Desktop/maze.png"), marea.getGrid(), 10);
		}
		
		Runnable r = new Runnable() {
			@Override
			public void run() {
				while(algo.getStatus() != 100){
					sbar.setStatusBar(algo.getStatus());
				}
				sbar.setStatusBar(algo.getStatus());
			}
		};
	}

}
